using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace StupidSpaceThing
{
	internal static partial class LevelSystem
	{
		private static Quad level2_borderQuad;
		private static bool level2_completed;

		private static void level2() {
			level2_completed = false;

			level2_borderQuad = new Quad(Game1.SelectionTexture);
			level2_borderQuad.Position = new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2, 0, 0);
			level2_borderQuad.Color = new Color(147, 112, 219, 100);
			level2_borderQuad.Scale = 87;
			
			TradePortal tradePortal = new TradePortal(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2, 0, Game1.CurMap.Height * Game1.CurMap.CellSize - 20), (float)(Math.PI / 2), 100, 1);
			Faction.None.Add(tradePortal);

			new Green(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2, 0, 20));
			new Red(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2, 0, Game1.CurMap.Height * Game1.CurMap.CellSize - 60));
			Faction.Red.Enemies = FactionColor.Green;
			new Yellow(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2 - 100, 0, Game1.CurMap.Height * Game1.CurMap.CellSize - 80));
			Faction.Yellow.Enemies = FactionColor.Green;
			new Blue(new Vector3(Game1.CurMap.Width * Game1.CurMap.CellSize / 2 + 100, 0, Game1.CurMap.Height * Game1.CurMap.CellSize - 80));
			Faction.Blue.Enemies = FactionColor.Green;

			Turret._ProductionCost = 100000;
			Scorpion._ProductionTime = 2000;

			int totalTurrets = 10;
			float interval = (float)(Math.PI / (totalTurrets - 1));
			for( int i = 0; i < totalTurrets; i++ ) {
				Faction.Green.Add(new Turret(new Vector3(Faction.Green.Hive.Position.X + ((float)Math.Cos(i * interval)) * 50, 0, Faction.Green.Hive.Position.Z + ((float)Math.Sin(i * interval)) * 50)));
			}

			int totalMines = 20;
			interval = (float)(Math.PI / (totalMines - 1));
			for( int i = 0; i < totalMines; i++ ) {
				Mine mine = new Mine(new Vector3(Faction.Green.Hive.Position.X + ((float)Math.Cos(i * interval)) * 80, 0, Faction.Green.Hive.Position.Z + ((float)Math.Sin(i * interval)) * 80));
				mine.DamageMultiplier = 5;
				mine.RangeMultiplier = 2;
				Faction.Green.Add(mine);
			}

			
			Game1.Player = new Player(Faction.Green.Hive.Position + new Vector3(0, 0, 20));
			Game1.Player.Entity.Armor = 4;
			Game1.Player.CanChangeFaction = false;
			Game1.Player.CanControlMines = true;
			
			Faction.Green.Add(Game1.Player);
		}

		private const int maxDist = 80;
		private static void level2_Update() {
			if( Faction.Green.Hive.Entity.Dead ) Game1.Player.Die();
			float distance = Vector3.Distance(level2_borderQuad.Position, Game1.Player.Position);
			if( distance > maxDist ) {
				float direction = (float)Math.Atan2(level2_borderQuad.Position.Z - Game1.Player.Position.Z, level2_borderQuad.Position.X - Game1.Player.Position.X);
				Game1.Player.Entity.Position = level2_borderQuad.Position + new Vector3(-(float)(Math.Cos(direction) * (maxDist - 0.5f)), Game1.Player.Position.Y, -(float)(Math.Sin(direction) * (maxDist - 0.5f)));
				Game1.Player.Entity.UpdatePositionMatrix();
				Game1.Player.Entity.ColObject.UpdateBodyPosition();
				Game1.Player.Entity.ColObject.Body.LinearVelocity = Vector2.Zero;
			}
		}

		private static void level2_Draw(Camera camera) {
			level2_borderQuad.Draw(camera);
		}


		public static void Level2_Completed() {
			if( level2_completed ) return;
			level2_completed = true;
			Map.Cell freeCell = null;
			Faction.Green.Hive.Entity.Heal(100);
			Game1.Player.Entity.Heal(100);
			for( int i = 0; i < 30; i++ ) {
				freeCell = Game1.CurMap.GetFreeCell(Faction.Green.Hive.Position, 20);
				Faction.Green.Add(new Scorpion(new Vector3(freeCell.X * Game1.CurMap.CellSize, 0, freeCell.Z * Game1.CurMap.CellSize)));
			}
			GuideSystem.ShowPriority(new TextGuide("The reinforcements are here, well done agent!", Game1.SpriteFont, TextAlign.Both, 1, Color.White, 5000, delegate() {
				Game1.Level = 3;
				Game1.Game.Reset();
			}), true);
		}
	}
}
